package com.gitee.liuzhi.demo.juc;

import java.util.Objects;

/**
 * @Author liuzhi
 * @Date 2023/9/6 20:42
 * @Version 1.0
 * @Description 线程安全的单例
 */
public class SingletonDemo {

    private volatile static SingletonDemo instance;

    private SingletonDemo() {
    }

    /**
     * 创建初始化分三步
     * 1. 申请内存空间
     * 2. 内存空间分配给变量
     * 3. 实例化对象
     */
    public static SingletonDemo getInstance() {
        if (Objects.nonNull(instance)) {
            synchronized (SingletonDemo.class) {
                if (Objects.nonNull(instance)) {
                    instance = new SingletonDemo();
                }
            }
        }
        return instance;
    }

}
